package com.jeesite.modules.home.web;

import com.jeesite.common.config.Global;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.common.service.SysOfficeUnitService;
import com.jeesite.modules.sys.entity.Office;
import com.jeesite.modules.sys.entity.Role;
import com.jeesite.modules.sys.utils.UserUtils;
import com.jeesite.modules.task.action.entity.DcSupTaskAction;
import com.jeesite.modules.task.dceconomicindicator.entity.DcSupTaskScore;
import com.jeesite.modules.task.dceconomicindicator.service.DcSupTaskScoreService;
import com.jeesite.modules.task.dict.entity.DcSupTaskInfoDict;
import com.jeesite.modules.task.dict.service.DcSupTaskInfoDictService;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 首页管理
 * @author: ciye
 * @date: 2022/3/3 16:04
 */
@Controller
@RequestMapping(value = "${adminPath}/home/homePage")
public class DcHomePageController extends BaseController {
    @Autowired
    private SysOfficeUnitService sysOfficeUnitService;
    @Autowired
    private DcSupTaskInfoDictService dcSupTaskInfoDictService;
    @Autowired
    private DcSupTaskScoreService dcSupTaskScoreService;
/*    *//**
     * 跳转到市领导工作台
     *//*
    @RequestMapping(value = {"leaderHome", ""})
    public String leaderHomePage(Model model) {
        DcSupTaskInfo dcSupTaskInfo = new DcSupTaskInfo();
        model.addAttribute("dcSupTaskInfo", dcSupTaskInfo);
        DcSupTaskInfo dcSupTaskInfo2 = new DcSupTaskInfo();
        model.addAttribute("dcSupTaskInfo2", dcSupTaskInfo2);
        return "modules/home/leaderHome";
    }*/

    /**
     * 跳转到督办首页
     */
    @RequestMapping(value = {"supIndex", ""})
    public String supIndex(Model model) {
        String point_office = Global.getConfig("sys.score.pointOffice");
        Office latestUnitByOffice = sysOfficeUnitService.getLatestUnitByOffice();
        model.addAttribute("point_office", "0");
        if(latestUnitByOffice.getOfficeCode()!=null){
            if(point_office.contains(latestUnitByOffice.getOfficeCode())){
                model.addAttribute("point_office", "1");
            }
        }
        return "modules/home/supIndex";
    }

    /**
     * 跳转到工作门户
     */
    @RequestMapping(value = {"workPortal", ""})
    public String workPortal(Model model) {
        String point_office = Global.getConfig("sys.score.pointOffice");
        String code ="";
        String createUnit ="";
        if(StringUtils.isNotBlank(point_office)){
            String[] split = point_office.split(",");
            for(String s :split){
                code =code+"\'"+s+"\',";
            }
            createUnit= code.substring(0,code.length()-1);
        }
        model.addAttribute("point",   0);
        Office latestUnitByOffice = sysOfficeUnitService.getLatestUnitByOffice();
        if(latestUnitByOffice.getOfficeCode()!=null){
            DcSupTaskScore dcSupTaskScore = new DcSupTaskScore();
            dcSupTaskScore.setCreateUnit(createUnit);
            dcSupTaskScore.setLeaderOrgCode(latestUnitByOffice.getOfficeCode());
            dcSupTaskScore.setTaskYear( Integer.parseInt(DateUtils.getYear())+"");
            List<DcSupTaskScore> list = dcSupTaskScoreService.findList(dcSupTaskScore);
            if(list!=null && list.size()>0){
                DcSupTaskScore dcSupTaskScore1 = list.get(0);
                model.addAttribute("point",   dcSupTaskScore1.getScore());
            }

        }

        return "modules/home/workPortal";
    }

    /**
     * 个人桌面页面
     */
    @RequestMapping(value = {"desktop", ""})
    public String desktop(Model model) {
        // 获取当前会话对象
        Session session = UserUtils.getSession();
        // 记录用户角色
        String roleCode = "";
        if (session.getAttribute("roleCode") != null) {
            // 获取用户选择的角色
            roleCode = session.getAttribute("roleCode").toString();
        }
        // 判断用户角色
        String point_office = Global.getConfig("sys.score.pointOffice");
        model.addAttribute("point_office", "0");
        Office latestUnitByOffice = sysOfficeUnitService.getLatestUnitByOffice();
        if(latestUnitByOffice.getOfficeCode()!=null){
            if(point_office.contains(latestUnitByOffice.getOfficeCode())){
                model.addAttribute("point_office", "1");
            }
        }
        if ("".equals(roleCode)) {
            // 初次登录时，roleCode为空，判断实际角色
            // 市领导角色
            for (Role role : UserUtils.getUser().getRoleList()) {
                if ("100016".equals(role.getRoleCode()) || "100017".equals(role.getRoleCode())) {
                    // 【市领导工作台】页面
                    DcSupTaskAction dcSupTaskAction = new DcSupTaskAction();
                    model.addAttribute("dcSupTaskAction", dcSupTaskAction);
                    DcSupTaskAction dcSupTaskAction2 = new DcSupTaskAction();
                    model.addAttribute("dcSupTaskAction2", dcSupTaskAction2);
                    getTaskType(model);
                    return "modules/home/leaderHome";
                }
            }
            // 督查单位领导 督查单位工作人员 角色
            for (Role role : UserUtils.getUser().getRoleList()) {
                if ("SUPLEADER".equals(role.getRoleCode()) || "OTSDEPLOY".equals(role.getRoleCode())) {
                    // 【督办首页】页面
                    return "modules/home/supIndex";
                }
            }

            String code ="";
            String createUnit ="";
            if(StringUtils.isNotBlank(point_office)){
                String[] split = point_office.split(",");
                for(String s :split){
                    code =code+"\'"+s+"\',";
                }
                createUnit= code.substring(0,code.length()-1);
            }
            model.addAttribute("point",   0);

            if(latestUnitByOffice.getOfficeCode()!=null){
                DcSupTaskScore dcSupTaskScore = new DcSupTaskScore();
                dcSupTaskScore.setCreateUnit(createUnit);
                dcSupTaskScore.setLeaderOrgCode(latestUnitByOffice.getOfficeCode());
                dcSupTaskScore.setTaskYear( Integer.parseInt(DateUtils.getYear())+"");
                List<DcSupTaskScore> list = dcSupTaskScoreService.findList(dcSupTaskScore);
                if(list!=null && list.size()>0){
                    DcSupTaskScore dcSupTaskScore1 = list.get(0);
                    model.addAttribute("point",   dcSupTaskScore1.getScore());
                }
            }

            // 【工作门户】页面
            return "modules/home/workPortal";
        } else if ("100016".equals(roleCode) || "100017".equals(roleCode)) {
            // 【市领导工作台】页面
            DcSupTaskAction dcSupTaskAction = new DcSupTaskAction();
            model.addAttribute("dcSupTaskAction", dcSupTaskAction);
            DcSupTaskAction dcSupTaskAction2 = new DcSupTaskAction();
            model.addAttribute("dcSupTaskAction2", dcSupTaskAction2);
            getTaskType(model);
            return "modules/home/leaderHome";
        } else if ("SUPLEADER".equals(roleCode) || "OTSDEPLOY".equals(roleCode)) {
            // 【督办首页】页面
            return "modules/home/supIndex";
        } else {
            // 【督办首页】页面
            return "modules/home/supIndex";
        }
    }

    /**
     * 获取重点工作任务类型，专题模块任务类型
     * @param model
     */
    public void getTaskType(Model model) {
        //重点
        List<DcSupTaskInfoDict> keyPoint = new ArrayList<>();
        //专项
        List<DcSupTaskInfoDict> special = new ArrayList<>();
        String keyStr ="( '国务院大督查' , '省委省政府督查室交办','市领导批示交办','市委市政府专题会议','市政府工作报告重点工作','人民网网友留言')";
        DcSupTaskInfoDict dcSupTaskInfoDict = new DcSupTaskInfoDict();
        dcSupTaskInfoDict.setDictStatus("0");
        dcSupTaskInfoDict.setOrderBy("dict_sort acs");
        List<DcSupTaskInfoDict> dicts = dcSupTaskInfoDictService.findList(dcSupTaskInfoDict);
        String chartKeyWork="";
        for(DcSupTaskInfoDict dict :dicts){
            String dictLabel = dict.getDictLabel();
            if( keyStr.contains(dictLabel)){
                if(StringUtils.equals("市领导批示交办",dictLabel)){
                    dict.setDictLabel("批示交办");
                    keyPoint.add(dict);
                }else if("市委市政府专题会议".equals(dictLabel)){
                    dict.setDictLabel("会议议定事项");
                    keyPoint.add(dict);
                }else{
                    if(StringUtils.isNotBlank(chartKeyWork)){
                        chartKeyWork = chartKeyWork+","+dict.getDictValue();
                    }else{
                        chartKeyWork = dict.getDictValue();
                    }
                    continue;
                }
            }else{
                special.add(dict);
            }
        }
        DcSupTaskInfoDict chartKeyWorkDict = new DcSupTaskInfoDict();
        chartKeyWorkDict.setDictLabel("分管重点工作");
        chartKeyWorkDict.setDictValue(chartKeyWork);
        keyPoint.add(chartKeyWorkDict);
        model.addAttribute("keyPointList",keyPoint);
        model.addAttribute("specialList",special);
    }
}
